package com.fyzk.activity.news;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.tyjc.lottery.R;

/**
 * Created by fanxs11 on 17/4/21.
 */
public class NewsTypeListAdapter extends ArrayAdapter<NewsTypeListBean.CategoryBean> {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private OnItemClickListener mListener;

    public NewsTypeListAdapter(Context context, int resource, OnItemClickListener listener) {
        super(context, resource);

        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mListener = listener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.view_news_category_item, parent, false);
            holder.mItem = (TextView) convertView;

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        NewsTypeListBean.CategoryBean info = getItem(position);
        holder.mItem.setText(info.getName());

        holder.mItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClicked(v, position);
            }
        });

        return convertView;
    }

    private static class ViewHolder {
        public TextView mItem;
    }

    public interface OnItemClickListener {
        void onItemClicked(View view, int position);
    }
}
